//
// Created by ziya on 22-8-20.
//
#include "../../include/common.h"
#include "../../include/core/coroutine.h"
#include "../../include/core/sched.h"

#include <signal.h>

extern coroutine_t* coroutines[TASK_SIZE];

static void* coroutine_fun(void* arg) {
    while (1) {
        INFO_PRINT("idle\n");
        sleep(1);
    }
}

static void sig_handler(int sig) {
    sigset_t sigset;
    sigemptyset(&sigset);

    switch (sig) {
        case SIGUSR1:
            // 清空信号
            sigaddset(&sigset, SIGUSR1);
            sigprocmask(SIG_UNBLOCK, &sigset, NULL);

            do_timer();

            break;
        default:
            ERROR_PRINT("未登记处理的信号\n");
            exit(-1);
    }
}

void env_init() {
    if (SIG_ERR == signal(SIGUSR1, sig_handler)) {
        ERROR_PRINT("注册信号处理函数失败\n");
        exit(-1);
    }

    coroutine_create(NULL, coroutine_fun, "idle", 1);
}